#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

#include "process.h"

double total = 2048.0;
double once = 0.5;
int base = 20;

//void download()
//{
//    double current = 0.0;
//    while(current < total)
//    {
//        int r = rand() % base + 1;
//        double speed = r * once; 
//        current += speed;
//        if(current >= total) current = total;
//        usleep(10000);
//
//        Process(total,current);
//
//     //   printf("test:%.1lf/%.1lf\r",current,total);
//     //   fflush(stdout);
//    }
//    printf("\n");
//}

typedef void (*flush_t)(double total,double current);

void download(flush_t f)
{
    double current = 0.0;
    while(current < total)
    {
        int r = rand() % base + 1;
        double speed = r * once; 
        current += speed;
        if(current >= total) current = total;
        usleep(10000);

        //Process(total,current);
        f(total,current);

     //   printf("test:%.1lf/%.1lf\r",current,total);
     //   fflush(stdout);
    }
    printf("\n");
}


int main()
{
    srand(time(NULL));
    //download();
    download(FlushProcess);
    return 0;
}
